<template>
  <div class="linktype">
    <el-tabs v-model="formData.productType" tab-position="left" @tab-click="handleClick">
      <el-tab-pane :label="item.name" :name="item.id" v-for="(item, index) in tabsList" :key="item.id">
        <div class="typetop">
          <el-button type="primary" @click="addLinkType((isSet = 0))">新增链接类型</el-button>
        </div>
        <div class="linktypelist">
          <el-table :data="linkTypeData" border style="width: 100%; margin-bottom: 20px">
            <el-table-column prop="id" label="ID"> </el-table-column>
            <el-table-column prop="name" label="链接类型"> </el-table-column>
            <el-table-column prop="updateName" label="添加人"> </el-table-column>
            <el-table-column prop="updateTime" label="添加时间"> </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button @click="addLinkType(scope.row, (isSet = 1))" type="text" size="small">编辑</el-button>
                <el-button @click="deleteLinkType(scope.row.id)" type="text" size="small">删除</el-button>
                <el-button v-show="scope.row.id == 'app_business_link'" @click="toLinkDetail(scope.row.id)" type="text" size="small">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            :page-size="formData.pageSize"
            :current-page="formData.pageNum"
            @current-change="SetPage"
            @size-change="handleSizeChange"
          ></el-pagination>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!-- 新增链接类型 -->
    <AddLinkType ref="edit" :typeDialogFormVisible="typeDialogFormVisible" @editorList="editList"></AddLinkType>
  </div>
</template>
<script>
import { getTypeList, deleteAppType } from "@/api/basisMG";
import AddLinkType from "./components/addLinkType";
export default {
  components: {
    AddLinkType,
  },
  data() {
    return {
      typeDialogFormVisible: false,
      linkTypeData: [],
      tabsList: [],
      formData: {
        type: 2,
        productType: "",
        pageSize: 10,
        pageNum: 1,
      },
      appFormData: {
        type: 0,
        productType: "",
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
    };
  },
  created() {
    this.getTypeList();
  },
  methods: {
    // 分页
    SetPage(e) {
      this.formData.pageNum = e;
      this.getLinkTypeList();
    },
    handleSizeChange (val) {
      this.formData.pageSize = val;
      this.getLinkTypeList();
    },
    handleClick(tab) {
      this.formData.productType = tab.name;
      this.getLinkTypeList();
    },
    async getTypeList() {
      const res = await getTypeList(this.appFormData);
      this.tabsList = res.data.content;
      this.formData.productType = this.tabsList[0].id;
      this.getLinkTypeList();
    },
    async getLinkTypeList() {
      const res = await getTypeList(this.formData);
      if (res.code === '200') {
        this.linkTypeData = res.data.content;
        this.total = res.data.total;
      } else {
        this.linkTypeData = [];
      }
    },
    deleteLinkType(id) {
      this.$confirm("确定要删除吗？删除后将无法恢复，请谨慎操作", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const data = await deleteAppType({id:id});
          if (data.code != 200) return this.$message.error("操作失败请稍后重试");
          this.getLinkTypeList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    toLinkDetail(id) {
      this.$router.push({
        path: "./linkDetail",
        query: { id: id, type: this.formData.productType },
      });
    },
    addLinkType(row) {
      const rowList = {};
      if (this.isSet == 0) {
        //点击添加
        rowList.isSet = 0;
      } else {
        //点击编辑
        rowList.name = row.name;
        rowList.productType = row.productType;
        rowList.id = row.id;
        rowList.dataUrl = row.dataUrl;
        rowList.pageUrl = row.pageUrl;
        rowList.isSet = 1;
      }
      this.$refs["edit"].showEdit(rowList);
    },
    editList(e) {
      this.getLinkTypeList();
    }
  },
};
</script>
<style lang="scss">
.linktype {
  padding: 20px;
  .typetop {
    padding-bottom: 20px;
  }
}
</style>
